package com.bff.gaia.streaming.api.mo.gaia.io.basiccoder;

import java.io.*;

public class DoubleArrayCoder {
	private Double[] val;
	private byte[] data;
	public DoubleArrayCoder(Double[] val) {
		this.val = val;
		this.makeBytes();
	}
	public DoubleArrayCoder(byte[] data) {
		this.data = data;
		this.makeDoubleArray();
	}
	private void makeDoubleArray() {
		DataInputStream din = new DataInputStream(new ByteArrayInputStream(this.data));

		try {
			int len = din.readInt();
			if (len == -1) {
				this.val = null;
			} else {
				this.val = new Double[len];

				for(int i = 0; i < len; ++i) {
					int dataLen = din.readInt();
					byte[] data = new byte[dataLen];
					din.readFully(data);
					DoubleCoder coder = new DoubleCoder(data);
					this.val[i] = coder.getDouble();
				}
			}
		} catch (IOException var7) {
			System.out.println("this is not the normal data");
			var7.printStackTrace();
		}

	}
	private void makeBytes() {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dataOutputStream = new DataOutputStream(bos);
		if (this.val == null) {
			try {
				dataOutputStream.writeInt(-1);
				dataOutputStream.writeDouble(0.0D);
			} catch (IOException var6) {
				var6.printStackTrace();
				this.data = null;
			}
		} else {
			try {
				dataOutputStream.writeInt(this.val.length);

				for(int i = 0; i < this.val.length; ++i) {
					DoubleCoder coder = new DoubleCoder(this.val[i]);
					byte[] data = coder.getBytes();
					dataOutputStream.writeInt(data.length);
					dataOutputStream.write(data);
				}
			} catch (IOException var7) {
				var7.printStackTrace();
				this.data = null;
			}
		}

		this.data = bos.toByteArray();
	}
	private static void printDoubleArray(Double[] array) {
		for(int i = 0; i < array.length; ++i) {
			System.out.print("" + array[i] + ",");
		}

		System.out.println();
	}
	public Double[] getDoubleArray() {
		return this.val;
	}
	public byte[] getBytes() {
		return this.data;
	}
}